package valuecomusa.fijibus.eticket.shorthaul;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import valuecomusa.fijibus.eticket.*;
import valuecomusa.fijibus.eticket.db.shorthaul.BusinessDBHandler;
import valuecomusa.fijibus.eticket.ws.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

//import android.view.Menu;

public class SetupJournal extends SunbeamActivity
{
	private static int counter = 1;
    private String TAG = "SetupJournal";
    private BusinessDBHandler db;

	public SetupJournal()
	{
		TAG = TAG + "." + counter++;
		if(counter > 1000000)
			counter = 1;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
        Log.d(TAG, "onCreate...");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_journal);
        //---
		doCommon();
        //---
        db = new BusinessDBHandler(this.getApplicationContext());
        List<BusAssignValue> busses = db.getBusesForStaff(WorkingSession.getInstance().loggedInConductor.ISN);
        if(busses == null || busses.size() == 0)
        {
            WorkingSession.getInstance().ShowToast(this, "No Bus assigned", Toast.LENGTH_SHORT);
            return;
        }
        BusValue busValue = db.getBusByISN(busses.get(0).BusISN);
        if(busValue == null)
        {
            WorkingSession.getInstance().ShowToast(this, "Bus does not found", Toast.LENGTH_SHORT);
            return;
        }

        //----------Bus-----------
        TextView busView = (TextView) findViewById(R.id.setup_journal_bus);
        busView.setText(busValue.toString());
        WorkingSession.getInstance().journalBus = busValue;
        //--Route init
        AdapterView.OnItemSelectedListener routeItemClick = new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long rowId)
            {
                try
                {
                    InputMethodManager inputManager = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);

                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
                catch(Exception ex){}
                //---
                RouteValue sel = (RouteValue) parent.getItemAtPosition(position);

                Log.d("routeItemClick", String.format("Select: %d - %s", sel.ISN, sel.Name));
                WorkingSession.getInstance().journalRoute = sel;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView)
            {
                Log.d("routeItemClick", "No item select.");
                WorkingSession.getInstance().journalRoute = null;
            }
        };
        List<RouteValue> routeList = db.getShortHaulRoutesOfBus(busValue.ISN);
        ArrayAdapter<RouteValue> adapter = new ArrayAdapter<RouteValue>(SetupJournal.this,
                android.R.layout.simple_dropdown_item_1line, routeList);
        //---
        Spinner xx = (Spinner)findViewById(R.id.setup_journal_route);
        xx.setOnItemSelectedListener(routeItemClick);
        xx.setAdapter(adapter);

        //--Driver
        AdapterView.OnItemSelectedListener driverItemClick = new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long rowId)
            {
                ConductorValue sel = (ConductorValue) parent.getItemAtPosition(position);

                Log.d("driverItemClick", String.format("Select: %d - %s", sel.ISN, sel.UserName));
                WorkingSession.getInstance().journalDriver = sel;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView)
            {
                Log.d("driverItemClick", "No item select.");
                WorkingSession.getInstance().journalDriver = null;
            }
        };

        List<ConductorValue> driverList = db.getDriverOfBus(WorkingSession.getInstance().journalBus.ISN);
        ArrayAdapter<ConductorValue> adapter2 = new ArrayAdapter<ConductorValue>(this,
                android.R.layout.simple_dropdown_item_1line, driverList);
        //---
        xx = (Spinner)findViewById(R.id.setup_journal_driver);
        xx.setAdapter(adapter2);
        //---
        xx.setOnItemSelectedListener(driverItemClick);

        Button btnClick = (Button)findViewById(R.id.setup_journal_route_click);
        btnClick.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Spinner xx = (Spinner)findViewById(R.id.setup_journal_route);
                xx.performClick();
            }
        });
        //--
        btnClick = (Button)findViewById(R.id.setup_journal_driver_click);
        btnClick.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Spinner xx = (Spinner)findViewById(R.id.setup_journal_driver);
                xx.performClick();
            }
        });
        if(driverList.size() <= 1)
            btnClick.setVisibility(View.GONE);

        //---button submit
        Button submit = (Button)findViewById(R.id.setup_journal_submit);
        submit.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if(WorkingSession.getInstance().journalBus == null ||
                        WorkingSession.getInstance().journalRoute == null ||
                        WorkingSession.getInstance().journalDriver == null)
                {
                    WorkingSession.getInstance().ShowToast(SetupJournal.this,
                            "Please select Bus, Route and Driver",
                            Toast.LENGTH_LONG);
                    return;
                }

                Log.d(TAG, "Begin go to Main screen...");

                JournalValue jv = new JournalValue();
                jv.BusISN = WorkingSession.getInstance().journalBus.ISN;
                jv.ConductorISN = WorkingSession.getInstance().loggedInConductor.ISN;
                jv.Timestamp = Utilities.DateToString(new Date());
                jv.JournalID = Calendar.getInstance().getTimeInMillis();
                jv.DriverISN = WorkingSession.getInstance().journalDriver.ISN;
                jv.DeviceId = Utilities.getDeviceInfo();
                jv.Latitude = 0; //missed here
                jv.Longitude = 0; //missed here
                jv.RouteISN = WorkingSession.getInstance().journalRoute.ISN;
                jv.SyncISN = -1;

                long rs = db.addJournal(jv);
                jv.ISN = (int)rs;

                WorkingSession.getInstance().setupJournal = jv;

                String cardNumber = db.getRuntimeValues("defaultCard");

                Intent next = new Intent(SetupJournal.this, Main.class);
                if (cardNumber != null && cardNumber.length() > 0)
                    next.putExtra("INPUT_DIRECT_CHECK_IN", 1);
                startActivity(next);
				//--
                Log.d(TAG, "Done");
				finish();
                /*//--
                MemberValue noCardCustomer = db.getMemberByCard(cardNumber);
                if (noCardCustomer != null)
                {
                    WorkingSession.getInstance().currentPassenger = noCardCustomer;
                    //--
                    Intent input = new Intent(SetupJournal.this, CheckInV2Stage.class);
                    input.putExtra("CHECK-IN_TYPE", Declarations.CHECK_IN_TYPE_NO_CARD);
                    startActivity(input);
                }
                else
                {
                    Intent next = new Intent(SetupJournal.this, Main.class);
                    startActivity(next);
                }*/


            }
        });
	}

    @Override
    protected void onStart()
    {
        super.onStart();
        //--
        Log.d(TAG, "onStart...");

    }

	@Override
	protected void onDestroy()
	{
		Log.d(TAG, "onDestroy...");
		super.onDestroy();
	}
}
